//
//  ContainerViewController.swift
//  HHSwift
//
//  Created by 华宏 on 2020/9/1.
//  Copyright © 2020 huahong. All rights reserved.
//

import UIKit

class ContainerViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

       
        let vc1 = TestViewController()
        vc1.view.backgroundColor = .brown
        vc1.view.frame = CGRect(origin: self.view.frame.origin, size: CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height*0.5))
        self.addChild(vc1)
        self.view.addSubview(vc1.view)
        
        let vc2 = TestViewController()
        vc2.view.backgroundColor = .cyan
        self.addChild(vc2)
        
//        vc1.removeFromParent()
//        vc1.willMove(toParent: self)
//        vc1.didMove(toParent: self)
        
       
        let swipe = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeAction(sender:)))
        swipe.direction = .left
        self.view.addGestureRecognizer(swipe)
        
        let swipe1 = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeAction(sender:)))
        swipe1.direction = .right
        self.view.addGestureRecognizer(swipe1)
        
        
    }
    
    @objc func swipeAction(sender:UISwipeGestureRecognizer) {
       
        let vc1 = self.children.first
        let vc2 = self.children.last
        
        var fromVC : UIViewController!
        var toVC : UIViewController!
        
        if sender.direction == .left {
            fromVC = vc1
            toVC = vc2
        }else if sender.direction == .right {
           fromVC = vc2
           toVC = vc1
        }
        
        self.transition(from: fromVC, to: toVC, duration: 1, options: .transitionFlipFromLeft, animations: {
            
        }) { (complate) in
            
        }
        
        
    }
    
}
